package senpec.qc.harmony.system.department;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import senpec.qc.harmony.base.mybatisplus.IBaseEntity;
import senpec.qc.harmony.base.mybatisplus.IBaseServiceImpl;
import senpec.qc.harmony.base.mybatisplus.Result;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
@RequiredArgsConstructor
public class DepartmentServiceImpl extends IBaseServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    private final DepartmentMapper departmentMapper;

    @Override
    public Result<Department> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        switch (columnName) {
            case "id" -> queryWrapper.eq(Department::getId, columnValue);
            case "label" -> queryWrapper.eq(Department::getLabel, columnValue);
        }
        Department department = departmentMapper.selectOne(queryWrapper);
        List<Department> departments = getListByPid(department.getId());
        department.setIsRoot(department.getPid() == 0);
        department.setIsLeaf(departments.isEmpty());
        return Result.ok(department);
    }

    @Override
    public Result<List<Department>> getTree() {
        List<Department> departments = createRoot();
        departments.forEach(department -> department.setChildren(getListByPid(department.getId())));
        return Result.ok(departments);
    }

    @Override
    public Result<List<Department>> getCascade() {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Department::getPid, 0L);
        List<Department> departments = departmentMapper.selectList(lambdaQueryWrapper);
        departments.forEach(department -> department.setChildren(getListByPid(department.getId())));
        return Result.ok(departments);
    }

    private List<Department> getListByPid(Long pid) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Department::getPid, pid);
        List<Department> departments = departmentMapper.selectList(lambdaQueryWrapper);
        departments.forEach(department -> department.setChildren(getListByPid(department.getId())));
        return departments;
    }

    @Override
    public Set<Long> getChildrenIdsById(Long id) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Department::getPid, id);
        List<Department> children = departmentMapper.selectList(lambdaQueryWrapper);
        List<Long> childrenIds = children.stream().map(IBaseEntity::getId).toList();
        List<Long> ids = new ArrayList<>(childrenIds);
        ids.add(id);
        childrenIds.forEach(item -> ids.addAll(getChildrenIdsById(item)));
        return new HashSet<>(ids);
    }

    private List<Department> createRoot() {
        List<Department> departments = new ArrayList<>();
        Department department = new Department();
        department.setId(0L);
        department.setIsRoot(true);
        department.setLabel("组织架构");
        departments.add(department);
        return departments;
    }


}